

<template>
  <div class="goods-sku">
    <dl v-for="item in goods.specs" :key="item.id">
      <dt>{{ item.name }}</dt>
      <dd>
        <template v-for="val in item.values" :key="val.name">
          <!-- 图片类型规格 -->
          <img v-if="val.picture" :src="val.picture" :title="val.name" @click="changeSelectedStatus(item,val)" :class="{selected: val.selected,disabled: val.disabled}">
          <!-- 文字类型规格 -->
          <span v-else @click="changeSelectedStatus(item,val)" :class="{selected: val.selected,disabled: val.disabled}">{{ val.name }}</span>
        </template>
      </dd>
    </dl>
  </div>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import axios from 'axios'
import powerSet from './power-set'
// 商品数据
const goods = ref({})
let pathMap={};
const getGoods = async () => {
  // 1135076  初始化就有无库存的规格
  // 1369155859933827074 更新之后有无库存项（蓝色-20cm-中国）
  const res = await axios.get('http://pcapi-xiaotuxian-front-devtest.itheima.net/goods?id=1369155859933827074')
  goods.value = res.data.result
  console.log(goods.value);
  pathMap=getPathMap(goods.value);
  console.log(pathMap);
  initDisabledStatus(goods.value.specs,pathMap);
}
onMounted(() => getGoods())
function changeSelectedStatus(item,val){
  if(val.disabled){
    return;
  }
  if(val.selected==true){
    val.selected=false;
  }
  else{
    item.values.forEach((arr) => {
      arr.selected=false;
    });
    val.selected=true;
  }
  // console.log(getSelectedValues(goods.value.specs));
  updateDisabledStatus(goods.value.specs,pathMap);
  let index= getSelectedValues(goods.value.specs).findIndex(item=>item===undefined);
  if(index>-1){
    console.log('error');
  }
  else{
    let key=getSelectedValues(goods.value.specs).join('-');
    let skuId=pathMap[key];
    let good=goods.value.skus.find(item=>item.id==skuId);
    console.log(good);
  }
}
function getPathMap(goods){
  let pathMap={};
  let effectiveSkus=goods.skus.filter((good)=>{
    if(good.inventory>0){
      return true;
    }
    return false;
  });
  effectiveSkus.forEach(sku=>{
    let selectedValArr=sku.specs.map((item)=>{
      return item.valueName;
    });
    let valueArrPowerSet = powerSet(selectedValArr);
    valueArrPowerSet.forEach((arr)=>{
      let key=arr.join('-');
      if(pathMap[key]){
        pathMap[key].push(sku.id);
      }
      else{
        pathMap[key]=[sku.id];
      }
    })
  })
  return pathMap;
}
function initDisabledStatus(specs,pathMap){
  specs.forEach((spec)=>{
    spec.values.forEach((val)=>{
      if(pathMap[val.name]){
        val.disabled=false;
      }
      else{
        val.disabled=true;
      }
    })
  })
}

function getSelectedValues(specs){
let arr=[];
specs.forEach((spec)=>{
  let selectedVal= spec.values.find((item)=>item.selected);
  arr.push(selectedVal?selectedVal.name:undefined);
})
return arr
}

function updateDisabledStatus(specs,pathMap){
  specs.forEach((spec,index)=>{
    let selectedValues=getSelectedValues(specs);
    spec.values.forEach((val)=>{
      selectedValues[index]=val.name;
      let key=selectedValues.filter(val=>val).join('-');
      if(pathMap[key]){
        val.disabled=false
      }
      else{
        val.disabled=true;
      }
    })
  })
}
</script>
<style scoped lang="scss">
@mixin sku-state-mixin {
  border: 1px solid #e4e4e4;
  margin-right: 10px;
  cursor: pointer;

  &.selected {
    border-color: #27ba9b;
  }

  &.disabled {
    opacity: 0.6;
    border-style: dashed;
    cursor: not-allowed;
  }
}

.goods-sku {
  padding-left: 10px;
  padding-top: 20px;

  dl {
    display: flex;
    padding-bottom: 20px;
    align-items: center;

    dt {
      width: 50px;
      color: #999;
    }

    dd {
      flex: 1;
      color: #666;

      >img {
        width: 50px;
        height: 50px;
        margin-bottom: 4px;
        @include sku-state-mixin;
      }

      >span {
        display: inline-block;
        height: 30px;
        line-height: 28px;
        padding: 0 20px;
        margin-bottom: 4px;
        @include sku-state-mixin;
      }
    }
  }
}
</style>
